package com.wrupple.vegetate.server.domain;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import com.wrupple.vegetate.domain.CatalogDescriptor;
import com.wrupple.vegetate.domain.FieldDescriptor;

public class CatalogDescriptorImpl implements CatalogDescriptor, Serializable,Cloneable  {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7833192705685048429L;
	private String id, name, clazz, keyField;
	private String descriptiveField;
	private Map<String, FieldDescriptor> fields;
	
	public CatalogDescriptorImpl copy(){
		CatalogDescriptorImpl regreso = new CatalogDescriptorImpl();
		regreso.setClazz(clazz);
		regreso.setDescriptiveField(descriptiveField);
		regreso.setFields(fields);
		regreso.setId(id);
		regreso.setKeyField(keyField);
		regreso.setName(name);
		return regreso;
		
		
	}

	/**
	 * @return The ID of the human-readable most descriptive field of this
	 *         catalog, by default it's the keyField, but this may not always be
	 *         the most intuitive field.
	 */
	public String getDescriptiveField() {
		if (this.descriptiveField == null) {
			this.descriptiveField = this.keyField;
		}
		return descriptiveField;
	}

	/**
	 * @param descriptiveField
	 *            The ID of the human-readable most descriptive field of this
	 *            catalog
	 */
	public void setDescriptiveField(String descriptiveField) {
		this.descriptiveField = descriptiveField;
	}


	/**
	 * @return the keyField
	 */
	public String getKeyField() {
		return keyField;
	}

	/**
	 * @param keyField
	 *            the keyField to set
	 */
	public void setKeyField(String keyField) {
		this.keyField = keyField;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the clazz
	 */
	public String getClazz() {
		return clazz;
	}

	/**
	 * @param clazz
	 *            the clazz to set
	 */
	public void setClazz(String clazz) {
		this.clazz = clazz;
	}


	/**
	 * @param id
	 *            the Machine Readable id for the field
	 * @return
	 */
	public FieldDescriptor getFieldDescriptor(String id) {
		return this.fields.get(id);
	}

	/**
	 * @return a set of all the ids of all fields
	 */
	public Set<String> getFieldNames() {
		return this.fields.keySet();
	}
	
	
	/**
	 * @param fields
	 *            the fields to set
	 */
	public void setFields(Map<String, FieldDescriptor> fields) {
		if (fields == null) {
		} else {
			FieldDescriptor curr = null;
			Set<String> set = fields.keySet();
			for (String key : set) {
				curr = fields.get(key);
				if (curr == null) {
				} else {
					((FieldDescriptorImpl)curr).setId(key);
				}
			}
		}
		this.fields = fields;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "CatalogDescriptor [clazz=" + clazz + ", fields=" + fields + ", id=" + id + ", keyField=" + keyField
				+ ", name=" + name +  "]";
	}

	@Override
	public Collection<FieldDescriptor> getFields() {
		return fields.values();
	}

}
